package com.itheima.reggie.controller;

import com.itheima.reggie.common.PageBean;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * order订单控制层
 */
@RestController
@RequestMapping("/order")
public class OrderController {


    @Autowired(required = false)
    private OrderService orderService;

    @Autowired
    private HttpSession session;
    /**
     * 订单页分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @RequestMapping("/page")
    public R<PageBean> findOrderPage(int page, int pageSize, String number, String beginTime , String endTime){
        return orderService.findOrderPage(page,pageSize,number,beginTime,endTime);
    }

    /**
     * 获取用户中心页最新订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<PageBean> findUserPage(int page,int pageSize){
        User loginUser = (User) session.getAttribute("loginUser");
        Long id = loginUser.getId();
        return orderService.findUserPage(page,pageSize,id);
    }

    @PostMapping("/submit")
    public R<Orders> submitOrder(@RequestBody Orders orders, HttpServletRequest request, HttpServletResponse response) throws IOException {
        User loginUser = (User) session.getAttribute("loginUser");
        orders.setUserId(loginUser.getId());
        orders.setUserName(loginUser.getName());
        return orderService.submitOrder(orders);
    }

    /**
     * 查询订单是否已经支付
     * @param
     * @return
     */
    @RequestMapping("/findOrderStatus")
    public R<String> findOrderStatus(String oid){
       return orderService.findOrderByNumber(oid).getStatus() == 2?R.success("支付成功"):R.error("用户未支付");
    }
}
